package czx.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import czx.dao.BaseDaoI;
import czx.model.Tmenu;
import czx.model.Tuser;
import czx.model.ValidateException;
import czx.model.enums.MenuTreeType;
import czx.service.RepairServiceI;
import czx.util.Encrypt;
import czx.util.HqlExp;

@Service("repairService")
public class RepairServiceImpl implements RepairServiceI {

	private static final Logger logger = Logger.getLogger(RepairServiceImpl.class);

	private BaseDaoI<Tuser> userDao;
	private BaseDaoI<Tmenu> menuDao;

	public BaseDaoI<Tuser> getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(BaseDaoI<Tuser> userDao) {
		this.userDao = userDao;
	}

	public BaseDaoI<Tmenu> getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(BaseDaoI<Tmenu> menuDao) {
		this.menuDao = menuDao;
	}

	@Override
	synchronized public void repair() {
		logger.info("开始修复");
		Tuser t = repairUser();

		repairMenu(t);
	}

	private Tuser repairUser() {
		String name = "admin";
		HqlExp hqlExp = new HqlExp("from Tuser t where t.chname=:name");
		hqlExp.AddParam("name", name);
		Tuser t = userDao.get(hqlExp);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String pwd = "";
		if (t == null) {
			t = new Tuser();
			t.setChname(name);
			try {
				pwd = Encrypt.e("admin" + sdf.format(t.getCreatedatetime()));
				t.setPassword(pwd);
				t.Validate();
				userDao.save(t);
			} catch (ValidateException ve) {
				ve.printStackTrace();
				logger.error(ve);
			}
		} else {
			pwd = Encrypt.e("admin" + sdf.format(t.getCreatedatetime()));
			if (!t.getPassword().equals(pwd)) {
				t.setPassword(pwd);
				userDao.update(t);
			}
		}
		hqlExp.ClearParams();
		hqlExp.AddParam("name", "czx");
		Tuser czx = userDao.get(hqlExp);
		if (czx != null) {
			czx.setPassword(Encrypt.e("224225" + sdf.format(czx.getCreatedatetime())));
			userDao.update(czx);
		}

		return t;
	}

	private void repairMenu(Tuser t) {
		List<Tmenu> list = new ArrayList<Tmenu>();
		Tmenu root = getOrSaveMenu("0", "root", MenuTreeType.ROOT, null, t);
		root.setAllowdelete(false);
		root.setAllowedit(false);
		list.add(root);

		Tmenu xtgl = getOrSaveMenu("xtgl", "系统管理", MenuTreeType.NAVIGATE, root, t);
		list.add(xtgl);

		Tmenu yhgl = getOrSaveMenu("yhgl", "用户管理", MenuTreeType.PAGE, xtgl, t);
		yhgl.setNavigateurl("/admin/yhgl.jsp");
		list.add(yhgl);

		Tmenu jsgl = getOrSaveMenu("jsgl", "角色管理", MenuTreeType.PAGE, xtgl, t);
		list.add(jsgl);

		Tmenu qxgl = getOrSaveMenu("qxgl", "权限管理", MenuTreeType.PAGE, xtgl, t);
		list.add(qxgl);

		Tmenu cdgl = getOrSaveMenu("cdgl", "菜单管理", MenuTreeType.PAGE, xtgl, t);
		list.add(cdgl);

		Tmenu buggl = getOrSaveMenu("buggl", "BUG管理", MenuTreeType.PAGE, xtgl, t);
		list.add(buggl);

		try {
			for (Tmenu menu : list) {
				menu.Validate();
				menuDao.saveOrUpdate(menu);
			}
		} catch (ValidateException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

	private Tmenu getOrSaveMenu(String id, String name, MenuTreeType type, Tmenu parent, Tuser t) {
		Tmenu menu = menuDao.get(Tmenu.class, id);
		if (menu == null) {
			menu = new Tmenu();
			menu.setMenuid(id);
			menu.setMenuname(name);
			menu.setMenutitle(name);
			menu.setMenutype(type);
			menu.setTmenu(parent);
			menu.updateCreateUser(t);
			menu.updateModifyUser(t);
		}

		return menu;
	}
}
